package framework.entity;

import java.util.ArrayList;
import java.util.StringTokenizer;


public class ACLComponent extends ACLEntity{

	public static String ACL_COMPONENT = "ACLCOMPONENT";
	
	private ArrayList<ACLInterface> interfacesReq;
	private ArrayList<ACLInterface> interfacesPro;

	public ACLComponent(String n) {
		super(n);
		interfacesReq = new ArrayList<ACLInterface>();
		interfacesPro = new ArrayList<ACLInterface>();
	}

	public ACLComponent(String n, ArrayList<ACLInterface> r,
			ArrayList<ACLInterface> p) {
		super(n);
		if (r == null)
			interfacesReq = new ArrayList<ACLInterface>();
		else
			interfacesReq = r;
		if (p == null)
			interfacesPro = new ArrayList<ACLInterface>();
		else
			interfacesPro = p;
	}

	public ACLComponent(String str, int mode) {
		super(str.substring(0, str.indexOf('[')));
		interfacesReq = new ArrayList<ACLInterface>();
		interfacesPro = new ArrayList<ACLInterface>();
		
		StringTokenizer tok = new StringTokenizer(str, "|");
		tok.nextToken();
		String s = tok.nextToken();

		while (!s.equals("@PRO")) {
			ACLInterface inter = new ACLInterface(s, 1);
			addInterfaceReq(inter);
			s = tok.nextToken();
		}
		s = tok.nextToken();
		while (tok.hasMoreTokens()) {
			ACLInterface inter = new ACLInterface(s, 1);
			addInterfacePro(inter);
			s = tok.nextToken();
		}

	}

	public void addInterfaceReq(ACLInterface req) {
		interfacesReq.add(req);
	}

	public void addInterfacePro(ACLInterface pro) {
		interfacesPro.add(pro);
	}

	public Boolean containsInterfaceReq(String n) {
		for (ACLInterface s : interfacesReq) {
			if (s.getName().equals(n))
				return true;
		}
		return false;
	}

	public Boolean containsInterfacePro(String n) {
		for (ACLInterface s : interfacesPro) {
			if (s.getName().equals(n))
				return true;
		}
		return false;
	}

	public String toString() {

		String s = getName() + "[@REQ|";
		;
		for (ACLInterface str : interfacesReq) {
			s = s + str.toString();
			s = s + "|";
		}
		s = s + "@PRO|";
		for (ACLInterface str : interfacesPro) {
			s = s + str.toString();
			s = s + "|";
		}
		s = s + "]";
		return s;
	}

	public ArrayList<ACLInterface> getInterfacesReq() {
		return interfacesReq;
	}

	public ArrayList<ACLInterface> getInterfacesPro() {
		return interfacesPro;
	}

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return ACL_COMPONENT;
	}

	

}
